﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MPI;

namespace SVM {
    public class MPIServerClientTask {
        public MPIServerClientTask() {
            InitClients();
            InitTaskStatistics();
        }

        private void InitClients() {
            ClientIDs = new int[comm.Size - 1];
            for (int i = 0; i < ClientIDs.Length; i++) {
                ClientIDs[i] = i + 1;
            }
        }
        private void InitTaskStatistics() {
            TaskReceived = ClientIDs.ToDictionary(o => o, o => 0);
            TaskSened = ClientIDs.ToDictionary(o => o, o => 0);
        }
        public bool HasClients {
            get {
                InitClients();
                return ClientIDs.Length > 0;
            }
        }
        public int[] ClientIDs { get; private set; }
        Intracommunicator comm = Communicator.world;
        public Intracommunicator Comm { get { return comm; } }
        public bool IsServer { get { return comm.Rank == 0; } }
        public Dictionary<int, int> TaskReceived { get; private set; }
        public Dictionary<int, int> TaskSened { get; private set; }
        public void ResetTaskStatistics() {
            InitTaskStatistics();
        }
        public void ReceiveOneTaskFromServer(int clientId) {
            TaskReceived[clientId] = TaskReceived[clientId] + 1;
        }
        public void SendOneTaskToServer(int clientId){
            TaskSened[clientId] = TaskSened[clientId] + 1;
        }
    }
}
